package com.jiaquyun.jcyx.main.mine

import android.Manifest
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.app.AppConstance
import com.jiaquyun.jcyx.app.JCApplication
import com.jiaquyun.jcyx.jetpack.mode.MineViewModel
import com.jiaquyun.jcyx.popup.DialogProtocolPopup
import com.jiaquyun.jcyx.utils.LocationUtils
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import com.tbruyelle.rxpermissions3.RxPermissions
import kotlinx.android.synthetic.main.activity_mine_shops.*
import org.greenrobot.eventbus.EventBus

class MineShopsActivity : OwnerViewModeActivity<MineViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(MineShopsActivity::class.java)
        }
    }

    private val protocolHintPopup: DialogProtocolPopup by lazy {
        DialogProtocolPopup(this,
            onConfirm = {
                protocolContentPopup.setContentText().showPopupWindow()
                protocolHintPopup.dismiss()
            },
            onCancel = {
                protocolHintPopup.dismiss()
                ActivityUtils.finishAllActivities()
            },
            onProtocol = { title, content ->

            })
    }
    private val protocolContentPopup: DialogProtocolPopup by lazy {
        DialogProtocolPopup(this,
            onConfirm = {
                SPStaticUtils.put(AppConstance.KEY_NAME_PROTOCOL, true)
                protocolContentPopup.dismiss()

                val rxPermissions = RxPermissions(this)
                rxPermissions.request(Manifest.permission.ACCESS_COARSE_LOCATION).subscribe { t ->
                    if (!t) {
                        toast("定位权限关闭，无法获取地理位置")
                        viewModel.userShopsList("0", "0")
                    } else{
                        val location = LocationUtils.getInstance().getLocations(this)
                            .split(",")
                        viewModel.userShopsList(location[1], location[0])
                    }
                }
            },
            onCancel = {
                SPStaticUtils.put(AppConstance.KEY_NAME_PROTOCOL, false)
                protocolContentPopup.dismiss()
                protocolHintPopup.setHintText().showPopupWindow()
            },
            onProtocol = { title, id ->
                MineRichTextActivity.openId(activity = this, title = title, id = id)
            })
    }

    private val mShopsListAdapter = ShopsListAdapter()

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_shops

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.white, view = flMineShopsBar)
    }

    override fun initView() {

        ivAppBack.setOnClickListener { onBackPressed() }

        rvMineShopsBody.adapter = mShopsListAdapter

        tvMineShopsGo.setOnClickListener {
            val shops = viewModel.userShopsLiveDate.value?.data?.data
                ?: return@setOnClickListener
            if (shops.isNullOrEmpty())
                return@setOnClickListener
            val userShops = shops[mShopsListAdapter.selectPosition]
            SPStaticUtils.put(AppConstance.SPS_KEY_USER_SHOPS, userShops.toString())
            EventBus.getDefault().post(userShops)
            finish()
        }
    }

    override fun initData() {

        if (SPStaticUtils.getBoolean(AppConstance.KEY_NAME_PROTOCOL,false)) {
            val rxPermissions = RxPermissions(this)
            rxPermissions.request(Manifest.permission.ACCESS_COARSE_LOCATION).subscribe { t ->
                if (!t) {
                    toast("定位权限关闭，无法获取地理位置")
                    viewModel.userShopsList("0", "0")
                } else{
                    val location = LocationUtils.getInstance().getLocations(this)
                        .split(",")
                    viewModel.userShopsList(location[1], location[0])
                }
            }
        } else {
            protocolContentPopup.setContentText().showPopupWindow()
        }
    }

    override fun initViewMode() {
        viewModel.userShopsLiveDate.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mShopsListAdapter.setNewInstance(data)
        })
    }

    override fun onBackPressed() {
        if (JCApplication.userShops == null) {
            ActivityUtils.finishAllActivities()
        } else {
            super.onBackPressed()
        }
    }

}